package com.xiaoyu.client.ui.activity.help;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.forum.ForumReplyOneRecyAdapter;
import com.xiaoyu.client.adapter.seek.SeekReplyOneRecyAdapter;
import com.xiaoyu.client.model.seek.SeekUserBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.forum.ForumDetailActivity;
import com.xiaoyu.client.ui.activity.main.mine.my_info.FansActivity;
import com.xiaoyu.client.ui.activity.main.mine.my_info.FocusoncountActivity;
import com.xiaoyu.client.ui.activity.main.mine.sign_and_help.MyCollectFragment;
import com.xiaoyu.client.ui.activity.main.mine.sign_and_help.MyHelpsFragment;
import com.xiaoyu.client.ui.activity.main.mine.sign_and_help.MyReplyFragment;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.utils.CCircleImageView;
import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author jx
 * @date 创建时间 2018/10/22
 * @Description 我的  -- 我的求助
 */
public class UserSeekInfoActivity extends BaseActivity {
    public static final String USER_ID = "user_seek_info_userid";
    @BindView(R.id.activity_seek_user_info_photo)
    CCircleImageView photoImg;//用户头像
    @BindView(R.id.activity_seek_user_info_nick_name)
    TextView nameTxt;//昵称
    @BindView(R.id.activity_seek_user_info_attention_num)
    TextView attentionNumTxt;//关注
    @BindView(R.id.activity_seek_user_info_fans_num)
    TextView fansNumTxt;//粉丝
    @BindView(R.id.activity_seek_user_info_signature)
    TextView signatureTxt;//个性签名
    private ViewPager mViewPager;
    private List<Fragment> mFragments;
    private String[] mTitles = new String[]{"求助", "回复", "收藏"};
    private TabLayout mTabLayout;
    private String userId;//用户id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_helps_info);
        parseIntent();
        setOccupyViewLinearGradient();
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserInfo();
    }


    private void parseIntent() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_SEEK_DETAIL) {
                userId = bundle.getString(HelpDetailActivity.USER_ID);
            }
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_SEEK_DETAIL_ONE_REPLY) {
                userId = bundle.getString(SeekReplyOneRecyAdapter.USER_ID);
            }
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_FORUM_DETAIL) {
                userId = bundle.getString(ForumDetailActivity.USER_ID);
            }
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_FORUM_DETAIL_ONE_REPLY) {
                userId = bundle.getString(ForumReplyOneRecyAdapter.USER_ID);
            }
        }
    }

    private void initView() {
        // 第一步，初始化ViewPager和TabLayout
        mViewPager = findViewById(R.id.viewpager);
        mTabLayout = findViewById(R.id.tabs);

        //上拉下拉刷新设置
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsHeader(context);
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });

        setupViewPager();

    }

    private void setupViewPager() {
        mFragments = new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putString(USER_ID, userId);
        MyHelpsFragment helpsFragment = new MyHelpsFragment();
        helpsFragment.setArguments(bundle);
        MyReplyFragment replyFragment = new MyReplyFragment();
        replyFragment.setArguments(bundle);
        MyCollectFragment collectFragment = new MyCollectFragment();
        collectFragment.setArguments(bundle);
        mFragments.add(helpsFragment);
        mFragments.add(replyFragment);
        mFragments.add(collectFragment);

        // 第二步：为ViewPager设置适配器
        CommonFragmentPagerAdapter adapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mViewPager.setAdapter(adapter);
        // 第三步：将ViewPager与TableLayout 绑定在一起
        mTabLayout.setupWithViewPager(mViewPager);
    }

    //返回键
    @OnClick(R.id.activity_my_helps_return)
    public void backClick() {
        onBackPressed();
    }


    /**
     * 获取用户信息（求助的个人中心信息）
     */
    private void getUserInfo() {
        NetworkManager.getSeekUserInfo(userId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseSeekUserSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void parseSeekUserSuc(String result) {
        SeekUserBean bean = new Gson().fromJson(result, SeekUserBean.class);

        Glide.with(this).load(bean.getSeekUser().getUserphoto()).into(photoImg);
        nameTxt.setText(bean.getSeekUser().getNickname());
        attentionNumTxt.setText(bean.getSeekUser().getFocusoncount() + "关注");
        fansNumTxt.setText(bean.getSeekUser().getFanscount() + "粉丝");
        if (TextUtils.isEmpty(bean.getSeekUser().getIndividuation())) {
            signatureTxt.setText("输入你的个性签名");
        } else {
            signatureTxt.setText(bean.getSeekUser().getIndividuation());
        }
    }

    //关注
    @OnClick(R.id.activity_seek_user_info_attention_num)
    public void attentionNumClick() {

        Intent intent = new Intent(this, FocusoncountActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(ClientConstants.FROM, ClientConstants.FROM_SEEK_INFO);
        bundle.putString(USER_ID, userId);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    //粉丝
    @OnClick(R.id.activity_seek_user_info_fans_num)
    public void fansNumClick() {
        Intent intent = new Intent(this, FansActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(ClientConstants.FROM, ClientConstants.FROM_SEEK_INFO);
        bundle.putString(USER_ID, userId);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
